
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.dao.ILeavetypeDAO;
import com.wyzz.hr.pojo.Leavetype;

public class LeavetypeBOImpl implements ILeavetypeBO {

	public LeavetypeBOImpl() {
	}

	public Leavetype getLeavetype(String inputLtNo) {
		String fetch[] = null;
		return (Leavetype) lt_DAO.loadObject(com.wyzz.hr.pojo.Leavetype.class,
				inputLtNo, fetch, new boolean[0]);
	}

	public boolean isTypeExist(String inputLtNo) {
		DetachedCriteria detachedcriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		detachedcriteria.add(Restrictions.eq(Leavetype.PROP_LT_NO, inputLtNo));
		return lt_DAO.findRowCountByCriteria(detachedcriteria) > 0;
	}

	public Leavetype getAnnualLeave() {
		DetachedCriteria detachedcriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		detachedcriteria.add(Restrictions.eq(Leavetype.PROP_LT_SPECIAL_CAT,
				new Integer(1)));
		List re = lt_DAO.findByCriteria(detachedcriteria);
		if (re != null && re.size() == 1)
			return (Leavetype) re.get(0);
		else
			return null;
	}

	public Leavetype getLeavetypeByCat(Integer ltSpecialCat) {
		DetachedCriteria detachedcriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		detachedcriteria.add(Restrictions.eq(Leavetype.PROP_LT_SPECIAL_CAT,
				ltSpecialCat));
		List re = lt_DAO.findByCriteria(detachedcriteria);
		if (re != null && re.size() == 1)
			return (Leavetype) re.get(0);
		else
			return null;
	}

	public List FindAllLeaveType() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		dc.addOrder(Order.asc(Leavetype.PROP_LT_SORT_ID));
		return lt_DAO.findByCriteria(dc);
	}

	public String addLeaveType(Leavetype leaveType) {
		String trimmedLeavetypeName;
		trimmedLeavetypeName = leaveType.getLtName().trim();
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		dc.add(Restrictions.eq(Leavetype.PROP_LT_NAME, trimmedLeavetypeName));
		List oldLeavetype = lt_DAO.findByCriteria(dc);
		if (!oldLeavetype.isEmpty())
			return "EXISTED";
		if (leaveType.getLtSpecialCat().intValue() != 20) {
			List leaveTypeSp = lt_DAO.exeHqlList((new StringBuilder()).append(
					" from Leavetype where ltSpecialCat=").append(
					leaveType.getLtSpecialCat()).toString());
			if (!leaveTypeSp.isEmpty())
				return "LEAVE_TYPE_EXISTED";
		}
		if (leaveType.getLtSpecialCat().intValue() == 11) {
			int sp = 11;
			List leaveTypeSp = lt_DAO.exeHqlList((new StringBuilder()).append(
					" from Leavetype where ltSpecialCat=").append(sp)
					.toString());
			if (!leaveTypeSp.isEmpty())
				return "DUPLICATE_TIAOXIU";
		}
		try {
			leaveType.setLtName(trimmedLeavetypeName);
			leaveType.setLtSortId(Integer.valueOf(0));
			lt_DAO.saveObject(leaveType);
			return "SUCC";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "FAIL";
	}

	public String delLeaveType(Class name, String id) {
		StringBuffer buffer = new StringBuffer();
		try {
			List check = lt_DAO.exeHqlList((new StringBuilder()).append(
					"select count(*) from Leaverequest where lrLtNo = '")
					.append(id).append("'").toString());
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				buffer
						.append("\u6709\u8BF7\u5047\u8BB0\u5F55\u5173\u8054\u5230\u8BE5\u8BF7\u5047\u7C7B\u578B\uFF01");
			else
				lt_DAO.deleteObject(com.wyzz.hr.pojo.Leavetype.class, id);
		} catch (Exception e) {
			buffer.append((new StringBuilder()).append(
					"\u7CFB\u7EDF\u5F02\u5E38\uFF1A").append(e.getMessage())
					.toString());
		}
		return buffer.toString();
	}

	public String updateLeaveType(Leavetype leaveType) {
		List temp = findLeaveTypeList(leaveType);
		if (!temp.isEmpty())
			return "EXISTED";
		if (leaveType.getLtSpecialCat().intValue() != 20) {
			List leaveTypeSp = lt_DAO.exeHqlList((new StringBuilder()).append(
					" from Leavetype where ltSpecialCat=").append(
					leaveType.getLtSpecialCat()).append(" and ltNo<>'").append(
					leaveType.getLtNo()).append("'").toString());
			if (!leaveTypeSp.isEmpty())
				return "LEAVE_TYPE_EXISTED";
		}
		if (leaveType.getLtSpecialCat().intValue() == 11) {
			int sp = 11;
			List leaveTypeSp = lt_DAO.exeHqlList((new StringBuilder()).append(
					" from Leavetype where ltSpecialCat=").append(sp).append(
					" and ltNo<>'").append(leaveType.getLtNo()).append("'")
					.toString());
			if (!leaveTypeSp.isEmpty())
				return "DUPLICATE_TIAOXIU";
		}
		Leavetype oldleaveType = (Leavetype) lt_DAO.loadObject(
				com.wyzz.hr.pojo.Leavetype.class, leaveType.getLtNo(), null,
				new boolean[0]);
		if (oldleaveType != null) {
			String trimmedLeavetypeName = leaveType.getLtName().trim();
			oldleaveType.setLtName(trimmedLeavetypeName);
			oldleaveType.setLtInclHoliday(leaveType.getLtInclHoliday());
			oldleaveType.setLtDesc(leaveType.getLtDesc());
			oldleaveType.setLtBalForward(leaveType.getLtBalForward());
			oldleaveType.setLtBalForwardDayLimit(leaveType
					.getLtBalForwardDayLimit());
			oldleaveType.setLtBalForwardHourLimit(leaveType
					.getLtBalForwardHourLimit());
			oldleaveType.setLtBalForwardPerLimit(leaveType
					.getLtBalForwardPerLimit());
			oldleaveType.setLtBalForwardRounding(leaveType
					.getLtBalForwardRounding());
			oldleaveType.setLtDaysForRelease(leaveType.getLtDaysForRelease());
			oldleaveType.setLtDaysOfYear(leaveType.getLtDaysOfYear());
			oldleaveType.setLtHoursForRelease(leaveType.getLtHoursForRelease());
			oldleaveType.setLtHoursOfYear(leaveType.getLtHoursOfYear());
			oldleaveType.setLtMaxApplyDays(leaveType.getLtMaxApplyDays());
			oldleaveType.setLtMaxApplyHours(leaveType.getLtMaxApplyHours());
			oldleaveType.setLtMinApplyDays(leaveType.getLtMinApplyDays());
			oldleaveType.setLtMinApplyHours(leaveType.getLtMinApplyHours());
			oldleaveType.setLtOtherParameter(leaveType.getLtOtherParameter());
			oldleaveType.setLtReleaseMethod(leaveType.getLtReleaseMethod());
			oldleaveType.setLtReleaseRounding(leaveType.getLtReleaseRounding());
			oldleaveType.setLtSpecialCat(leaveType.getLtSpecialCat());
			oldleaveType.setLtNeedComments(leaveType.getLtNeedComments());
			oldleaveType.setLtApplyByDay(leaveType.getLtApplyByDay());
			lt_DAO.updateObject(oldleaveType);
			return "SUCC";
		} else {
			return "ERROR";
		}
	}

	public void saveLeavetypeByBatch(String ids[]) {
		if (ids == null)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			lt_DAO.exeHql((new StringBuilder()).append(
					"update Leavetype set ltSortId=").append(sort).append(
					" where id ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public List findLeaveTypeList(Leavetype leaveType) {
		DetachedCriteria detachedcriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		detachedcriteria.add(Restrictions.eq("ltName", leaveType.getLtName()
				.trim()));
		detachedcriteria.add(Restrictions.ne("ltNo", leaveType.getLtNo()));
		List re = lt_DAO.findByCriteria(detachedcriteria);
		return re;
	}

	public List findLeaveTypeByName(String name) {
		DetachedCriteria detachedcriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Leavetype.class);
		detachedcriteria.add(Restrictions.eq("ltName", name.trim()));
		List re = lt_DAO.findByCriteria(detachedcriteria);
		return re;
	}

	public ILeavetypeDAO getLt_DAO() {
		return lt_DAO;
	}

	public void setLt_DAO(ILeavetypeDAO lt_DAO) {
		this.lt_DAO = lt_DAO;
	}

	private ILeavetypeDAO lt_DAO;
}
